// 读文件 ，累加 ， 写到总成绩.txt里面
// 1. 导入fs模块
const fs = require('fs')

const path = require('path')

// 2. 读取
const fullPath = path.join(__dirname, './成绩.txt')
console.log(fullPath)
const res = fs.readFileSync(fullPath, 'utf-8')
console.log(res)
// 把字符串转换成数组
const arr = res.split(',')
console.log(arr)

const resArr = arr.map(el => {
    const rRes = el.split('=')
    console.log(+rRes[1])
    return +rRes[1]
})
console.log(resArr)
// 3. 累加
const total = resArr.reduce((acc, cur) => {
    return acc + cur
})
console.log(total)

// 4. 写入新文件
const fullPath2 = path.join(__dirname, './总成绩.txt')
fs.writeFileSync(fullPath2, total + '')